package com.example.android.services

import android.util.Log
import com.example.android.extensions.StringBody
import com.example.android.models.Dorm
import com.example.android.models.DormInfo
import com.example.android.models.Dorm_1
import com.example.android.utils.Api
import com.example.android.utils.JsonUtil
import com.example.android.utils.models.Form
import com.example.android.utils.models.MyResult
import com.example.android.viewmodels.DormViewModel
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

/**
 * @ClassName SelectBedService
 * @Author Lucas
 * @Date 2022/6/5
 * @Description
 **/
class SelectBedService(
    private val _Api: Api,
    private val _JsonUtil: JsonUtil,
    private val _HashMapStorage: HashMap<String, String>
    ) {

    suspend fun GetDorm(model: DormViewModel):MyResult<ArrayList<Dorm>>{
        val token = _HashMapStorage["token"] ?: return MyResult()

        val response = _Api.Get("/dorm/selectDormNumber") {
            it.addHeader("jwtToken", token)
        }.StringBody()
        Log.d(toString(), response)
        val result = _JsonUtil.FromJsonObject(response).AsMyResult<ArrayList<Form>>()
        val dormList : ArrayList<Dorm> = ArrayList<Dorm>()
        result.Data?.forEach {
            dormList.add(Dorm(
                //it["dormId"] as String,
                it["region"] as String,
                //it["ridgepole"] as String,
                //it["floor"] as String,
                it["dormNumber"] as String))
                //it["bed"] as Int,
                //it["id"] as Int,
                //it["choose"] as Int))
        }
        model.getDormId().postValue(dormList)
        Log.d(toString(), dormList.toString())
        return MyResult(result.Code, result.Message, dormList)

    }


    suspend fun GetBed(region:String,dormNumber:String):MyResult<ArrayList<Dorm_1>>{


        val response = _Api.Get("/dorm/getBedNumber?region=${region}&&dormNumber=${dormNumber}").StringBody()
        Log.d(toString(), response)
        val result = _JsonUtil.FromJsonObject(response).AsMyResult<ArrayList<Form>>()
        val dormList : ArrayList<Dorm_1> = ArrayList<Dorm_1>()
        result.Data?.forEach {
            dormList.add(Dorm_1(
                it["dormId"] as String,
//                it["region"] as String,
//                it["ridgepole"] as String,
//                it["floor"] as String,
//                it["dormNumber"] as String,
//                it["bed"] as Int,
//                it["id"] as Int,
                it["choose"] as Int))
        }
        //model.getDormId().postValue(dormList)
        Log.d(toString(), dormList.toString())
        return MyResult(result.Code, result.Message, dormList)

    }


    suspend fun PostDormId(dormId:String):MyResult<String>{

        val token = _HashMapStorage["token"] ?: return MyResult()
        val form = Form()
        form["dormId"] = dormId

        val response = _Api.Post("/dorm/choice?dormId=${dormId}",_JsonUtil.ToJson(form)) {
            it.addHeader("jwtToken", token)
        }.StringBody()
        val result = _JsonUtil.FromJsonObject(response).AsMyResult<String>()

        return result
//        when (resForm["code"]) {
//            MyResult.LOG_IN_SUCCESS -> {
//                val token = resForm.Get("data").Get<String>("token")
//                _HashMapStorage["token"] = token
//                return resForm.AsMyResult()
//            }
//        }
//        return resForm.AsMyResult()
    }

    suspend fun DeleteBed():MyResult<String>{
        val token = _HashMapStorage["token"] ?: return MyResult()

        val response = _Api.Post("/dorm/deleteBedInformation", {}) {
            it.addHeader("jwtToken", token)
        }.StringBody()
        val result = _JsonUtil.FromJsonObject(response).AsMyResult<String>()

        return result
    }


    suspend fun Check():MyResult<DormInfo>{
        val token = _HashMapStorage["token"] ?: return MyResult()

        val response = _Api.Get("/dorm/select") {
            it.addHeader("jwtToken", token)
        }.StringBody()

        Log.d(toString(), response)

        val result = _JsonUtil.FromJsonObject(response).AsMyResult<Form>()
        when (result.Code) {
            MyResult.OK -> {
                val dorm = DormInfo(
                    result.Data?.get("dormId") as String,
                    result.Data?.get("region") as String,
//                    result.Data?.get("ridgepole") as String,
//                    result.Data?.get("floor") as String,
                    result.Data?.get("dormNumber") as String,
                    result.Data?.get("id") as Int,
                    result.Data?.get("bed") as Int,
                    result.Data?.get("choose") as Int
                )
                Log.d(toString(),dorm.toString())
                return MyResult(result.Code, result.Message, dorm)
                //return MyResult(result.Code, result.Message,InfoList)
            }

            else -> return MyResult()
        }
    }

}